package com.kye.express.business.notice.notice_center;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.base.Logger;
import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatActivity;
import com.kye.express.databinding.ActivityNoticeCenterBinding;
import com.kye.lib.view.statuslayout.StatusLayout;
import com.kye.model.bean.notice.NotificationListResultBean;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.List;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.MultiTypeAdapter;

public class NoticeCenterActivity extends BaseAppCompatActivity implements NoticeCenterContract.NoticeCenterView {

    protected ActivityNoticeCenterBinding mDataBinding;
    protected NoticeCenterContract.NoticeCenterPresenter mPresenter;
    protected MultiTypeAdapter mAdapter;

    public static Intent newIntent(Context context) {
        return new Intent(context, NoticeCenterActivity.class);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_notice_center);
        mPresenter = new NoticeCenterContract.NoticeCenterPresenter();
        mPresenter.onCreate(savedInstanceState);
        mPresenter.bind(this);
        initToolbar();
        initViews(savedInstanceState);
        initEvents(savedInstanceState);
        initData(savedInstanceState);
        Logger.d("notice center onCreate");
    }

    private void initToolbar() {
        setSupportActionBar(mDataBinding.toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setTitle("通知中心");
        }
        mDataBinding.toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initViews(Bundle savedInstanceState) {
        mAdapter = new MultiTypeAdapter();
        mAdapter.register(NotificationListResultBean.NotificationListItemBean.class, new NoticeItemViewBinder());
        mDataBinding.rvNotices.setLayoutManager(new LinearLayoutManager(this));
        mDataBinding.rvNotices.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mDataBinding.rvNotices.setItemAnimator(new DefaultItemAnimator());
        mDataBinding.rvNotices.setAdapter(mAdapter);
        mDataBinding.refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mPresenter.LoadMoreNotices();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mPresenter.refreshNotices();
            }
        });
    }

    private void initEvents(Bundle savedInstanceState) {
        RxView.clicks(mDataBinding.llEmpty.btEmptyRetry)
                .compose(this.bindToLifecycleDestroy())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        Logger.d("onEmpty");
                        mDataBinding.statusLayout.showLoadingView();
                    }
                });
        RxView.clicks(mDataBinding.llErrorNet.btErrorNetRetry)
                .compose(this.bindToLifecycleDestroy())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        Logger.d("onErrorNet");
                        mDataBinding.statusLayout.showLoadingView();
                    }
                });
        RxView.clicks(mDataBinding.llErrorServer.btErrorServerRetry)
                .compose(this.bindToLifecycleDestroy())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        Logger.d("onErrorServer");
                        mDataBinding.statusLayout.showLoadingView();
                    }
                });
        mDataBinding.statusLayout.setOnLoadingListener(new StatusLayout.OnLoadingListener() {
            @Override
            public void onLoading() {
                mPresenter.refreshNoticesFirst();
            }
        });
    }

    private void initData(Bundle savedInstanceState) {
        mDataBinding.statusLayout.showLoadingView();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        mPresenter.onSaveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }

    @Override
    public void refreshNoticesFirstSuccess(List<NotificationListResultBean.NotificationListItemBean> notificationList) {
        mAdapter.setItems(notificationList);
        mAdapter.notifyDataSetChanged();
        mDataBinding.statusLayout.showContentView();
    }

    @Override
    public void refreshNoticesSuccess(List<NotificationListResultBean.NotificationListItemBean> notificationList) {
        mDataBinding.refreshLayout.finishRefresh(true);
        mAdapter.setItems(notificationList);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void refreshNoticesFailed(Throwable e) {
        mDataBinding.refreshLayout.finishRefresh(false);
    }

    @Override
    public void loadMoreNoticesSuccess(List<NotificationListResultBean.NotificationListItemBean> notificationList) {
        mDataBinding.refreshLayout.finishLoadmore(true);
        int start = mAdapter.getItems().size();
        List<NotificationListResultBean.NotificationListItemBean> items = (List<NotificationListResultBean.NotificationListItemBean>) mAdapter.getItems();
        items.addAll(notificationList);
        mAdapter.notifyItemRangeInserted(start, items.size());
    }

    @Override
    public void loadMoreNoticesFailed(Throwable e) {
        mDataBinding.refreshLayout.finishLoadmore(false);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mPresenter.unbind();
        super.onDestroy();
    }

    @Override
    public ActivityNoticeCenterBinding getDataBinding() {
        return mDataBinding;
    }

    @Override
    public Context getContext() {
        return this;
    }
}
